﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SalaryMS
{
    public partial class SalaryMS : Form
    {
        public SalaryMS()
        {
            InitializeComponent();
            //set defalut accept button;
            defaultAcceptButton();
        }

        private void SalaryMS_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("你想退出系统吗？", "退出", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                Application.ExitThread();
            }
            else
            {
                e.Cancel = true;
            }
        }

        private void AddCmpInfo_ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new CmpAddUI().ShowDialog();
        }

        private void btnCmpSearch_Click(object sender, EventArgs e)
        {
            String sql = "SELECT CmpId,CmpName,Owner,EmpNumber,Contacter,ContacterPhone,Contractor,CreateDate,Remark FROM Company WHERE CmpName LIKE '%" + txtCmpName.Text.Trim().ToString() + "%'";
            DataTable dt = DBSession.ExecuteDataTable(sql, null);
            dgvCmpInfo.DataSource = dt;
        }

        private void defaultAcceptButton()
        {
            if (tabPanel.SelectedIndex == 0)
            {
                txtCmpName.Focus();
                this.AcceptButton = btnCmpSearch;
                String sql = "SELECT CmpId,CmpName,Owner,EmpNumber,Contacter,ContacterPhone,Contractor,CreateDate,Remark FROM Company";
                DataTable dt = DBSession.ExecuteDataTable(sql, null);
                dgvCmpInfo.DataSource = dt;
            }
            else if (tabPanel.SelectedIndex == 1)
            {
                txtEmpName.Focus();
                this.AcceptButton = btnEmpSearch;
                String sql = "SELECT EmpId,EmpName,BankAccount,CmpName,Emplyee.CreateDate,Emplyee.Remark FROM Emplyee "
                            + " LEFT JOIN Company on Emplyee.CmpId = Company.CmpId";
                DataTable dt = DBSession.ExecuteDataTable(sql, null);
                dgv_EmpInfo.DataSource = dt;
            }
            else if (tabPanel.SelectedIndex == 2) 
            {
                cmbCmp_S.Focus();
                this.AcceptButton = btnSalarySearch;
                String sql = "SELECT CmpId,CmpName FROM Company";
                DataTable dt = DBSession.ExecuteDataTable(sql, null);
                cmbCmp_S.DataSource = dt;
                cmbCmp_S.DisplayMember = "CmpName";
                cmbCmp_S.ValueMember = "CmpId";
                if (cmbCmp_S.SelectedValue != null && cmbCmp_S.SelectedValue.ToString().Length > 0)
                {
                    sql = "SELECT SalaryDate FROM SalaryDate WHERE CmpId='" + cmbCmp_S.SelectedValue.ToString() + "'";
                }
                else
                {
                    sql = "SELECT SalaryDate FROM SalaryDate";
                }
                DataTable dtSD = DBSession.ExecuteDataTable(sql, null);
                cmbSalaryDate_S.DataSource = dtSD;
                cmbSalaryDate_S.DisplayMember = "SalaryDate";
                cmbSalaryDate_S.ValueMember = "SalaryDate";
                lblMoneyTotal.Text = "0.00";
            }
        }

        private void tabPanel_SelectedIndexChanged(object sender, EventArgs e)
        {
            defaultAcceptButton();
        }

        private void UpdateCmpInfo_ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new CmpUpdateUI().ShowDialog();
        }

        private void AddEmp_ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new EmpAddUI().ShowDialog();
        }

        private void btnEmpSearch_Click(object sender, EventArgs e)
        {
            String sql = "SELECT EmpId,EmpName,BankAccount,CmpName,Emplyee.CreateDate,Emplyee.Remark FROM Emplyee "
                            + " LEFT JOIN Company on Emplyee.CmpId = Company.CmpId"
                            + " WHERE EmpName LIKE '%"+txtEmpName.Text.Trim().ToString()+"%'";
            DataTable dt = DBSession.ExecuteDataTable(sql, null);
            dgv_EmpInfo.DataSource = dt;
        }

        private void UpdateEmp_ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new EmpUpdateUI().ShowDialog();
        }

        private void AddSalaryDate_ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            new SalaryDateAddUI().ShowDialog();
        }

        private void InputEmpSalary_ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new SalaryEmpAddUI().ShowDialog();
        }

        private void QueryEmpSalary_ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new SalaryEmpSearch().ShowDialog();
        }

        private void btnSalarySearch_Click(object sender, EventArgs e)
        {
            String sql = "SELECT ID,SalaryDate,BankAccount,Money,EmpName FROM Salary WHERE SalaryDate='" + cmbSalaryDate_S.SelectedValue + "' AND CmpId='" + cmbCmp_S.SelectedValue + "'";
            DataTable dt = DBSession.ExecuteDataTable(sql, null);
            dgvSalary.DataSource = dt;
            sql = "SELECT sum(Money) as MoneyTotal FROM Salary WHERE SalaryDate='" + cmbSalaryDate_S.SelectedValue + "' AND CmpId='" + cmbCmp_S.SelectedValue + "'";
            dt = DBSession.ExecuteDataTable(sql, null);
            if (dt != null && dt.Rows.Count > 0)
            {
                lblMoneyTotal.Text = Double.Parse(dt.Rows[0][0].ToString()).ToString("#0.00");
            }
        }

        private void ExportEmpSalaryInfo_ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new SalaryExportUI().ShowDialog();
        }

        private void VerifyEmpSalary_ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new SalaryVerifyUI().ShowDialog();
        }

        private void cmbCmp_S_SelectedValueChanged(object sender, EventArgs e)
        {
            String sql = "";
            if (cmbCmp_S.SelectedValue != null && cmbCmp_S.SelectedValue.ToString().Length > 0)
            {
                sql = "SELECT SalaryDate FROM SalaryDate WHERE CmpId='" + cmbCmp_S.SelectedValue.ToString() + "'";
            }
            else
            {
                sql = "SELECT SalaryDate FROM SalaryDate";
            }
            DataTable dtSD = DBSession.ExecuteDataTable(sql, null);
            cmbSalaryDate_S.DataSource = dtSD;
            cmbSalaryDate_S.DisplayMember = "SalaryDate";
            cmbSalaryDate_S.ValueMember = "SalaryDate";
        }
    }


}
